package com.odm.communitymanage.ui.mine.MyPay

import android.view.View
import android.view.animation.DecelerateInterpolator
import android.widget.ImageView
import androidx.core.view.ViewCompat
import com.chad.library.adapter.base.entity.node.BaseNode
import com.chad.library.adapter.base.provider.BaseNodeProvider
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.odm.communitymanage.R
import com.odm.communitymanage.core.bean.MonthExpense

/**
 * @description:
 * @author: ODM
 * @date: 2020/4/20
 */
class MonthExpenseAdapter : BaseNodeProvider() {

    override val itemViewType: Int
        get() = 1
    override val layoutId: Int
        get() = R.layout.item_my_pay_title_month

    override fun convert(helper: BaseViewHolder, item: BaseNode) {
        val entity:MonthExpense ?= item as MonthExpense
        helper.setText(R.id.tv_time_MyPayTitle , entity?.time)
        val sum = item.sum + " 元"
        helper.setText(R.id.tv_sum_MyPayTitle , sum)
        setArrowSpin(helper , item , true)
    }

    override fun convert(helper: BaseViewHolder, item: BaseNode, payloads: List<Any>) {
        for (payload in payloads) {
            if (payload is Int && payload == MyPayAdapter.EXPAND_COLLAPSE_PAYLOAD) {
                // 增量刷新，使用动画变化箭头
                setArrowSpin(helper, item, true)
            }
        }
    }

    private fun setArrowSpin(
        helper: BaseViewHolder,
        data: BaseNode?,
        isAnimate: Boolean
    ) {
        val entity: MonthExpense ? = data as MonthExpense?
        val imageView = helper.getView<ImageView>(R.id.iv_guide_unfinished)
        if (entity?.isExpanded == true) {
            //已展开状态
            if (isAnimate){
                ViewCompat.animate(imageView).setDuration(200)
                    .setInterpolator(DecelerateInterpolator())
                    .rotation(180f)
                    .start()
            } else {
                imageView.rotation = 0f
            }
        } else {
            //未展开状态
            imageView.setImageResource(R.mipmap.icon_guide_arrow)
            imageView.rotation = 0f
        }
    }

    override fun onClick(
        helper: BaseViewHolder,
        view: View,
        data: BaseNode,
        position: Int
    ) {
        getAdapter()?.expandOrCollapse(
            position,
            true,
            true,
            MyPayAdapter.EXPAND_COLLAPSE_PAYLOAD
        )

    }
}